package com.chat.datea.fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chat.datea.R;
import com.chat.datea.adapter.EveryDayFragment_RecyclerView_Adapter;
import com.chat.datea.base.BaseFragment;
import com.chat.datea.base.Finnal_Base;
import com.chat.datea.bean.UserRecSongList;
import com.chat.datea.utils.data.DataUtil;
import com.chat.datea.utils.volley.VolleySingleton;
import com.google.gson.Gson;

import java.util.List;

/**
 * Created by xyb on 15/10/24.
 */
public class EveryDayFragment extends BaseFragment {
    private RecyclerView recyclerView;
    private EveryDayFragment_RecyclerView_Adapter recyclerViewAdapter;
    private VolleySingleton singleton;
    private List<UserRecSongList.ResultEntity.ListEntity> list;
    private TextView music_num,everyDay,day_num;
    private Toolbar toolbar;
    private String dayNum;


    public EveryDayFragment(String dayNum) {
       this.dayNum = dayNum;


    }

    @Override
    protected void initData() {
        everyDay.setText("星期"+DataUtil.getDataForString());
        day_num.setText(dayNum);
        singleton = VolleySingleton.getVolleySingleton(mActivity);
        initRecyclerView();
        toolbar.setNavigationIcon(R.mipmap.nav_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().getSupportFragmentManager().popBackStack();
            }
        });

    }

    private void initRecyclerView() {
        String url = Finnal_Base.apiUrl + Finnal_Base.method + "baidu.ting.song.userRecSongList" + Finnal_Base.format + Finnal_Base.page_no + "1" + Finnal_Base.page_size + "60";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                UserRecSongList userRecSongList = gson.fromJson(response, UserRecSongList.class);
                list = userRecSongList.getResult().getList();
                music_num.setText("共" + list.size() + "首歌");
                Log.e("EveryDayFragment", "list.size():" + list.size());
                recyclerViewAdapter = new EveryDayFragment_RecyclerView_Adapter(mActivity, list);
                recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
                recyclerView.setAdapter(recyclerViewAdapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        singleton.addQueue(request, "EveryDayFragment");
    }

    @Override
    protected void initView() {
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        music_num = (TextView) view.findViewById(R.id.music_num);
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        everyDay =  (TextView) view.findViewById(R.id.everyDay);
        day_num = (TextView) view.findViewById(R.id.day_num);
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.fragment_everyday, null);
        return view;
    }

}
